﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using Janus.Cores.Tools;

namespace Janus.Mes.WebClient.Areas.BaseData.Controllers.WorkCenter
{
    public class WorkCenterController : BaseController
    {
        // GET: BaseData/WorkCenter
        [HttpGet]
        public ActionResult Index()
        {
            return View(GetList(new FormCollection()));
        }
        /// <summary>
        /// 返回查询数据
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(GetList(collection));
        }

        /// <summary>
        /// 新建工作中心
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Create(string id)
        {
            WorkCenterVO wcv = null;
            ViewBag.IsCreate = true;
            if (!string.IsNullOrEmpty(id))
            {
                ViewBag.IsCreate = false;
                Querier query = new Querier(" WorkCCode = {@0} ", new List<object>() { id });
                var model = WorkCenterServices.FindOne(query);// WorkCenterServices.FindOne(query);
                
                wcv = model.ToEntity<WorkCenterVO>();
            }
            else
            {
                wcv = base.InitModel<WorkCenterVO>(wcv);
            }
            return View(wcv);
        }

        /// <summary>
        /// 新建工作中心
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Create(FormCollection form)
        {
            try
            {
                var result = false;
                WorkCenterVO model = new WorkCenterVO();
                model = base.SetModel<WorkCenterVO>(form);
                var workcenters = model.ToEntity<WorkCenterDTO>();
                result = WorkCenterServices.Save(workcenters) > 0;
                if (result)
                {
                    ajaxStr = base.AjaxResultSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }

        /// <summary>
        /// 删除作中心
        /// </summary>
        /// <param name="id">工作中心主键</param>
        /// <returns></returns>
        public string Delete(string id)
        {
            try
            {
                var result = true;
                Querier query = new Querier(" WorkCCode = {@0} ", new List<object>() { id });
                result = WorkCenterServices.Delete(query);
                if (result)
                {
                    ajaxStr = base.AjaxDeleteSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }

            return "{" + ajaxStr + "}";
        }

        // GET: BaseData/WorkCenter
        /// <summary>
        /// 工作中心详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Details(string id)
        {
            return Create(id);
        }

        public ActionResult AttachWorkUnit()
        {
            return View();
        }

        public ActionResult WorkCenterTree()
        {
            return View();
        }

        #region 私有方法
        protected IEnumerable<WorkCenterVO> GetList(FormCollection collection)
        {
            var searchParma = base.SearchPara<WorkCenterDTO>(collection);
            if (searchParma.Model.WorkCName.IsNotEmpty())
            {
                searchParma.Queriers.Where("WorkCName.Contains({@0})", searchParma.Model.WorkCName);
            }
            searchParma.Queriers.Where("IsDelete.Equals(0)");
            //可以继续判断 增加查询条件

            var result = base.FindAll<WorkCenterDTO>(searchParma, WorkCenterServices);
            if (!result.ErrorMessage.IsNotEmpty())
            {
                var list = result.Result.ToEntitys<WorkCenterVO>();// AutoMapper.Mapper.DynamicMap<List<WorkCenterVO>>(result.Result);
                return list;
            }
            else
            {
                return new List<WorkCenterVO>();
            }
        }
        #endregion
    }
}